package org.liubility.robot.test;

import com.jdragon.common.utils.FileUtils;
import kotlin.Unit;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.message.GroupMessageEvent;
import net.mamoe.mirai.message.data.*;

import java.io.File;

/**
 * @Author: Jdragon
 * @email: 1061917196@qq.com
 * @Date: 2020.09.04 15:15
 * @Description:
 */
public class ImagesListener extends SimpleListenerHost {
    @EventHandler
    public void onGroupMessage(GroupMessageEvent event) {
        MessageChain chain = event.getMessage();
        Group subject = event.getSubject();
        chain.forEachContent(msg -> {
            if (msg instanceof Image) {
                String s = event.getBot().queryImageUrl(((Image) msg));
                FileUtils.download(s, "D:/WEB/image.png");
                File file = new File("D:/WEB/image.png");
                if (file.exists()) {
                    Image image = subject.uploadImage(file);
                    MessageChainBuilder messageChainBuilder = new MessageChainBuilder();
                    messageChainBuilder.add(new At(event.getSender()));
                    messageChainBuilder.add(image);
                    messageChainBuilder.add("图片失效请点链接：" + event.getBot().queryImageUrl(image));
                    MessageChain messages = messageChainBuilder.build();
                    event.getSubject().sendMessage(messages);
                }
                subject.sendMessage(MessageUtils.newImage(((Image) msg).getImageId()));
            }
            return Unit.INSTANCE;
        });
    }
}
